//index.js
//获取应用实例
const app = getApp();
var util= require('../../utils/util.js');
Page({
  data: {
    allarrThings:[],
    arrThings:[],
    allarrDaojishi:[],
    arrDaojishi:[],
    daojishilength:0,
    thinglength:0,
    date:""
  },

// 计算天数
countDays:function(){
  this.setData({
    arrDaojishi:[]
  })
  for(var i=0;i<this.data.allarrDaojishi.length;i++) {
    var end_date = new Date(this.data.allarrDaojishi[i].daojishidate);
    var start_date = new Date()
    start_date.setHours(8);//设置为8点，让时间的时分秒保持一致
    start_date.setMinutes(0);
    start_date.setSeconds(0);
    start_date.setMilliseconds(0);
    // console.log("开始日期",start_date)
    // console.log("结束日期",end_date)
    //end_date.getTime()-1000*60*60*8)因为时间多了了8个小时，所以在这里减去多出来的8个小时，调整到到0点整来进行计算
    var daysint = (end_date.getTime()) - start_date.getTime();
    var days = parseInt(daysint / (1000 * 60 * 60 * 24));
    if((days)>=0){ //如果天数大于等于0，就插入到arrDaojishi数组中，否则忽略   
      var temp = 'allarrDaojishi['+i+'].countDays'
      this.setData({
        [temp]:days
      })
        this.data.arrDaojishi.push(this.data.allarrDaojishi[i]);
        this.setData({
          arrDaojishi:this.data.arrDaojishi
      })    
    }
    else{//如果小于0，就从数据库中删除掉这个倒计时
      wx.cloud.callFunction({
        name:"deletedaojishi",
        data:{
          _id:this.data.allarrDaojishi[i]._id
        }
      })
    }
  }     
},
  
//长按倒计时
longdaojishiListener:function(options){
  console.log(options.currentTarget.dataset._id)
  wx.showModal({
    title: '提示',
    content: '确定删除倒计时？',
    success (res) {
      if (res.confirm) {//点击确认，获取到倒计时的_id
       wx.cloud.callFunction({
         name:"deletedaojishi",
         data:{
           _id:options.currentTarget.dataset._id
         }
       }).then(res=>{
          wx.showToast({
            title: '成功删除',
            icon:'success',
            duration:1000
          })
       })
      } else if (res.cancel) {
      }
    }
  })
},

editListener:function(options){//编辑事件
  //console.log(options.currentTarget.dataset);
  var _id = options.currentTarget.dataset._id;
  var daojishid = options.currentTarget.dataset.daojishid;
  var duration = options.currentTarget.dataset.duration;
  var thingname = options.currentTarget.dataset.thingname;
  
  wx.navigateTo({
    url: '../../pages/creatething/creatething?_id='+_id+'&daojishid='+daojishid+'&duration='+duration+'&thingname='+thingname,
  })
},

//长按事件
longthingListener:function(options){
  console.log(options.currentTarget.dataset._id)
  wx.showModal({
    title: '提示',
    content: '确定删除事件？',
    success (res) {
      if (res.confirm) {//点击确认，获取到倒计时的_id
       wx.cloud.callFunction({
         name:"deletething",
         data:{
           _id:options.currentTarget.dataset._id
         }
       }).then(res=>{
          wx.showToast({
            title: '成功删除',
            icon:'success',
            duration:2000
          })
       })
      } else if (res.cancel) {
      }
    }
  })
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("首页用户信息",app.globalData.userInfo)
    var dateNow = util.formatTime(new Date());//获取当前执行这个计时的当天日期
    //console.log(dateNow.toString())
    var index = dateNow.indexOf(' ');
    dateNow = dateNow.substring(0,index )
     this.setData({
     date: dateNow,
     });
     console.log(this.data.date)
    //console.log(app.globalData.userInfo)  
  },

  //事件处理函数
  //点击开始时
  startListener:function(e){
    //传过来下标时
      var index = e.currentTarget.dataset.index;
      var thingname = this.data.arrThings[index].thingname;
      var duration = this.data.arrThings[index].duration;
      var _id = this.data.arrThings[index]._id;
      var daojishid = this.data.arrThings[index].daojishid;
      var daojishiname= this.data.arrThings[index].daojishiname;
      wx.redirectTo({
           url: '../jishi/jishi?thingname='+thingname+'&duration='+duration+'&_id='+_id+'&daojishid='+daojishid+'&daojishiname='+daojishiname
     })
    },


  onReady: function () {
  
  },


  onShow: function () {
   this.onPullDownRefresh();
  },


  onHide: function () {
    
  },


  onUnload: function () {
 
  },

  onPullDownRefresh: function () {
    //console.log(app.globalData.userInfo)  
    wx.cloud.callFunction({
      name: "getdaojishi",
      data:{}
    }).then(res=>{
      //console.log(res.result.data)
      this.setData({
        allarrDaojishi:res.result.data
        //daojishilength:res.result.data.length
      })
     // console.log("allarrDaojishi")
      //console.log(this.data.allarrDaojishi)
      this.countDays();
      //console.log("arrDaojishi")
      //console.log(this.data.arrDaojishi)
      this.setData({
        daojishilength:this.data.arrDaojishi.length
      })
      //console.log(this.data.daojishilength)
    });
    wx.cloud.callFunction({
      name:"getthing",
      data:{}
    }).then(res=>{
      //console.log(res.result.data)
      this.setData({
        arrThings:res.result.data,
        thinglength:res.result.data.length
      })
    }) 
  
  },

  onReachBottom: function () {
    
  },

  onShareAppMessage: function () {
    
  }
})
